package top.fyxxxx.downloader.utils;

import android.content.Context;
import android.widget.ListView;

import java.util.List;
import java.util.Map;

public class TaskListener implements DownloadListener{
    private Context context;
    private String filename,filepath,url;
    private Map<String,DownloadTask> map;
    private  SqlUtil sqlUtil=SqlUtil.getInstance(context);
    private DownloadTask task;
    private ListView listView;
    private List<History> list;
    public TaskListener(Context context, String filename, String filepath,String url, ListView listView,Map<String, DownloadTask>map, DownloadTask task){
        this.context=context;
        this.filename=filename;
        this.filepath=filepath;
        this.url=url;
        this.map=map;
        this.task=task;
        this.listView=listView;
    }
        @Override
        public void start() {
            map.put(filename,task);
            sqlUtil.insertHistory(new History(filename, sqlUtil.getDownloadPath()+"/"+filename,url,1,System.currentTimeMillis(),0,0));
            list=sqlUtil.getAllHistory();
            listView.setAdapter(new HistoryAdapter(context,map,list,listView));
        }

        @Override
        public void loading(long cur, long total) {
            History temp=sqlUtil.getHistory(filename);
            System.out.println("loading..."+cur+" "+total);
            sqlUtil.updateHistory(new History(filename,sqlUtil.getDownloadPath()+"/"+filename,temp.getUrl(),1,temp.getDate(),total,cur));
            list=sqlUtil.getAllHistory();
            listView.setAdapter(new HistoryAdapter(context,map,list,listView));
        }

        @Override
        public void complete(String path) {
            History temp=sqlUtil.getHistory(filename);
            sqlUtil.updateHistory(new History(filename,sqlUtil.getDownloadPath()+"/"+filename,temp.getUrl(),0,temp.getDate(),temp.getFileSize(),temp.getDownloadedSize()));
            list=sqlUtil.getAllHistory();
            listView.setAdapter(new HistoryAdapter(context,map,list,listView));
        }

        @Override
        public void fail(int code, String message) {
            History temp=sqlUtil.getHistory(filename);
            sqlUtil.updateHistory(new History(filename,sqlUtil.getDownloadPath()+"/"+filename,temp.getUrl(),3,temp.getDate(),temp.getFileSize(),temp.getDownloadedSize()));
            list=sqlUtil.getAllHistory();
            listView.setAdapter(new HistoryAdapter(context,map,list,listView));
        }

        @Override
        public void pause() {
            History temp=sqlUtil.getHistory(filename);
            sqlUtil.updateHistory(new History(filename,sqlUtil.getDownloadPath()+"/"+filename,temp.getUrl(),2,temp.getDate(),temp.getFileSize(),temp.getDownloadedSize()));
            list=sqlUtil.getAllHistory();
            listView.setAdapter(new HistoryAdapter(context,map,list,listView));
        }

        @Override
        public void loadfail(String message) {
            History temp=sqlUtil.getHistory(filename);
            sqlUtil.updateHistory(new History(filename,sqlUtil.getDownloadPath()+"/"+filename,temp.getUrl(),3,temp.getDate(),temp.getFileSize(),temp.getDownloadedSize()));
            list=sqlUtil.getAllHistory();
            listView.setAdapter(new HistoryAdapter(context,map,list,listView));
        }
}
